﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ShopMS.Common;
using System.IO;

namespace ShopMS.Shell
{
    /// <summary>
    /// Shell.xaml 的交互逻辑
    /// </summary>
    public partial class Shell : UserControl,IShell
    {
        private Dictionary<FunInfo, TabItem> functions = new Dictionary<FunInfo, TabItem>();
        private Window launcher;
        public void SetLauncher(Window launcher)
        {
            this.launcher = launcher;
        }

        public Shell()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            //winLogin win = new winLogin();
            //win.Owner = launcher;
            //win.WindowState = WindowState.Normal;
            //win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            //win.ShowInTaskbar = false;
            //if (win.ShowDialog() == true)
            //{
            //    Console.WriteLine(string.Format("{0}登录成功",ShopMS.Common.UserInfo.userName));
            //    ReflatorPlugins();
            //}

            ReflatorPlugins();
        }

        private void ReflatorPlugins()
        {
            List<FunInfo> funList = new List<FunInfo>();

            string[] files = Directory.GetFiles(System.IO.Directory.GetCurrentDirectory() + "\\plugins", "*.dll");
            files.ToList<string>().ForEach(
                t =>
                {
                    Console.WriteLine(string.Format("{0}", t));
                    System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(t);
                    //AppDomain.CurrentDomain.Load(assembly.GetName());
                    assembly.GetModules().ToList<System.Reflection.Module>().ForEach(
                        m =>
                        {
                            m.GetTypes().ToList<Type>().ForEach(
                                tp =>
                                {
                                    Console.WriteLine(string.Format("{0}", tp.Name));

                                    if (tp.GetInterface(typeof(IPlugin).Name) != null)
                                    {
                                        Console.WriteLine("plugin:" + tp.FullName);
                                        IPlugin plugin = (IPlugin)System.Activator.CreateInstance(tp);
                                        FunInfo info = plugin.GetFunInfo();
                                        info.funIcon = System.IO.Directory.GetCurrentDirectory() + "\\Resources\\Icon\\" + info.funIcon;
                                        info.funAssemblyName = assembly.GetName();
                                        info.funType = tp;
                                        funList.Add(info);
                                    }
                                }
                                );
                        }
                        );
                }
                );
            lstFunction.ItemsSource = funList.OrderBy(info => info.showOrder);
        }

        private void Function_MouseDown(object sender, MouseButtonEventArgs e)
        {
            FunInfo info = ((Grid)sender).DataContext as FunInfo;

            InvokeFunction(info);
        }

        private void InvokeFunction(FunInfo info)
        {
            TabItem ti;
            if (functions.ContainsKey(info))
            {
                ti = functions[info];
            }
            else
            {
                ti = new TabItem();
                ti.Header = info.funName;

                Button btn = new Button();

                System.Windows.Controls.UserControl control = System.Activator.CreateInstance(info.funType) as UserControl ;
                ti.Content = control;
                //control.Margin = new Thickness(0, 0, 0, 0);
                

                tcMain.Items.Add(ti);
                functions.Add(info, ti);
            }
            tcMain.SelectedItem = ti;
        }
    }
}
